package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.EmpSocItemAddDTO;
import org.jsola.hr.dto.EmpSocItemBatchDTO;
import org.jsola.hr.dto.EmpSocItemUpdateDTO;
import org.jsola.hr.query.EmpSocItemQuery;
import org.jsola.hr.service.IEmpSocItemService;
import org.jsola.hr.vo.EmpSocItemListVO;
import org.jsola.hr.vo.EmpSocItemVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 员工参保信息表子项
 *
 * @author zhr
 */
@RestController("hrEmpSocItemController")
@RequestMapping(HrConstants.API_PATH + "/v1/empSocItems")
public class EmpSocItemController implements IEmpSocItemControllerAPI {

    @Autowired
    private IEmpSocItemService empSocItemService;

    @Override
    @PostMapping
    public Result<EmpSocItemVO> save(@Valid @RequestBody EmpSocItemAddDTO empSocItemAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工参保信息表子项权限校验

        return Result.success(
                empSocItemService.save(empSocItemAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody EmpSocItemUpdateDTO empSocItemUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工参保信息表子项权限校验

        int count = empSocItemService.update(empSocItemUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @PostMapping(value = "/batch")
    public Result batch(@Valid @RequestBody EmpSocItemBatchDTO empSocItemBatchDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量操作权限校验

        // 批量操作，注意校验，有越权风险
        // 新增
        List<EmpSocItemAddDTO> empSocItemAddDTOList = empSocItemBatchDTO.getEmpSocItemAddDTOList();
        if (!CollectionUtils.isEmpty(empSocItemAddDTOList)) {
            empSocItemService.batchSave(empSocItemAddDTOList,tokenUser);
        }
        // 修改
        List<EmpSocItemUpdateDTO> empSocItemUpdateDTOList = empSocItemBatchDTO.getEmpSocItemUpdateDTOList();
        if (!CollectionUtils.isEmpty(empSocItemUpdateDTOList)) {
            empSocItemUpdateDTOList.parallelStream().forEach(
                    empSocItemUpdateDTO -> {
                        empSocItemService.update(empSocItemUpdateDTO,tokenUser);
                    }
            );
        }
        // 删除
        List<String> deleteEmpSocItemIdList = empSocItemBatchDTO.getDeleteEmpSocItemIdList();
        if (!CollectionUtils.isEmpty(deleteEmpSocItemIdList)) {
            empSocItemService.deleteByIds(tokenUser,deleteEmpSocItemIdList.toArray(new String[0]));
        }
        return Result.success();
    }


    @Override
    @DeleteMapping(value = "/{empSocItemId}")
    public Result<Integer> delete(@PathVariable String empSocItemId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工参保信息表子项权限校验

        int count = empSocItemService.deleteByIds(tokenUser, empSocItemId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] empSocItemIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工参保信息表子项权限校验

        int count = empSocItemService.deleteByIds(tokenUser, empSocItemIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{empSocItemId}")
    public Result<EmpSocItemVO> findById(@PathVariable String empSocItemId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工参保信息表子项权限校验

        return Result.success(
            empSocItemService.selectById(empSocItemId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<EmpSocItemListVO>> listAll(@Valid EmpSocItemQuery empSocItemQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工参保信息表子项权限校验

        return Result.success(
            empSocItemService.select(empSocItemQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<EmpSocItemListVO>> page(@Valid EmpSocItemQuery empSocItemQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            empSocItemService.selectPage(empSocItemQuery, tokenUser.getSiteId())
        );
    }


}

